using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using SamasDBI;
using SamasDBI.MySQL;
using SamasUtilities;

namespace EQEmuMysql
{
    public class Accounts
    {
        private string sName;
        private string sCharName;
        private string sSharedPlat;
        private string sPassword;
        private string sStatus;
        private string sLSAccountID;
        private string sGMSpeed;
        private string sRevoked;
        private string sMiniLoginIP;
        private string sHideMe;
        private string sRulesFlag;
        private int nID;
        private bool flgIsNew = false;
        private bool flgDeleted = false;

        private static int nMAXID = 0;

        public string Name
        {
            get { return sName; }
            set { sName = value; }
        }
        public string CharName
        {
            get { return sCharName; }
            set { sCharName = value; }
        }
        public string SharedPlat
        {
            get { return sSharedPlat; }
            set { sSharedPlat = value; }
        }
        public string Password
        {
            get { return sPassword; }
            set { sPassword = value; }
        }
        public string Status
        {
            get { return sStatus; }
            set { sStatus = value; }
        }
        public string LSAccountID
        {
            get { return sLSAccountID; }
            set { sLSAccountID = value; }
        }
        public string GMSpeed
        {
            get { return sGMSpeed; }
            set { sGMSpeed = value; }
        }
        public string Revoked
        {
            get { return sRevoked; }
            set { sRevoked = value; }
        }
        public string MiniLoginIP
        {
            get { return sMiniLoginIP; }
            set { sMiniLoginIP = value; }
        }
        public string HideMe
        {
            get { return sHideMe; }
            set { sHideMe = value; }
        }
        public string RulesFlag
        {
            get { return sRulesFlag; }
            set { sRulesFlag = value; }
        }        
        public int ID
        {
            get { return nID; }
            set { nID = value; }
        }

        public bool IsNew
        {
            get { return flgIsNew; }
        }


        public Accounts()
        {
            if (nMAXID == 0)
                nMAXID = GetMaxID();
            nMAXID++;

            sName = "";
            sCharName = "";
            sSharedPlat = "";
            sPassword = "";
            sStatus = "";
            sLSAccountID = "";
            sGMSpeed = "";
            sRevoked = "";
            sMiniLoginIP = "";
            sHideMe = "";
            sRulesFlag = "";
            nID = nMAXID;

            flgIsNew = true;
        }

        public Accounts(int ID)
        {
            MySqlConnection conGet = new MySqlConnection(MySQLRunner.ConnectionString);
            MySqlDataReader drGet = null;
            string sSQL = "";

            sSQL = "SELECT * FROM account WHERE id=" + ID.ToString();

            if (MySQLRunner.ExecuteQuery(sSQL, conGet, ref drGet) == true)
            {
                drGet.Read();

                if (drGet.HasRows == false)
                {
                    if (nMAXID == 0)
                        nMAXID = GetMaxID();

                    nMAXID++;

                    sName = "";
                    sCharName = "";
                    sSharedPlat = "";
                    sPassword = "";
                    sStatus = "";
                    sLSAccountID = "";
                    sGMSpeed = "";
                    sRevoked = "";
                    sMiniLoginIP = "";
                    sHideMe = "";
                    sRulesFlag = "";
                    nID = 0;

                    flgIsNew = true;
                }
                else
                {
                    sName = drGet.GetString(drGet.GetOrdinal("name"));
                    sCharName = drGet.GetString(drGet.GetOrdinal("charname")); ;
                    sSharedPlat = drGet.GetString(drGet.GetOrdinal("sharedplat"));
                    sPassword = "";
                    sStatus = drGet.GetString(drGet.GetOrdinal("status"));
                    sLSAccountID = drGet.GetString(drGet.GetOrdinal("lsaccount_id"));
                    sGMSpeed = drGet.GetString(drGet.GetOrdinal("gmspeed"));
                    sRevoked = drGet.GetString(drGet.GetOrdinal("revoked"));
                    sMiniLoginIP = drGet.GetString(drGet.GetOrdinal("minilogin_ip"));
                    sHideMe = drGet.GetString(drGet.GetOrdinal("hideme"));
                    sRulesFlag = drGet.GetString(drGet.GetOrdinal("rulesflag"));
                    

                    nID = ID;

                    flgIsNew = false;
                }
            }
            else
            {
                MessageBox.Show("Error connecting to the database.");
            }

            if (conGet.State == ConnectionState.Open)
            {
                if (drGet != null)
                    drGet.Close();

                conGet.Close();
            }
        }

        public void Delete()
        {
            flgDeleted = true;
        }

        public bool Save()
        {
            bool flgReturn;

            if (flgDeleted == false)
            {
                if (flgIsNew == true)
                    flgReturn = Add();
                else
                    flgReturn = Update();
            }
            else
            {
                flgReturn = DeleteObject();
            }

            if (flgReturn == true)
                flgIsNew = false;

            return flgReturn;
        }

        private bool Update()
        {
            MySqlConnection conUpdate = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;


            sSQL = "UPDATE account SET " +
                   "name='" + sName + "', " +
                   "charname='" + sCharName + "', " +
                   "sharedplat='" + sSharedPlat + "', " +   
                   "status='" + sStatus + "', " +
                   "lsaccount_id='" + sLSAccountID + "', " +
                   "gmspeed='" + sGMSpeed + "', " +
                   "revoked='" + sRevoked + "', " +
                   "minilogin_ip='" + sMiniLoginIP + "', " +
                   "hideme='" + sHideMe + "', " +
                   "rulesflag='" + sRulesFlag + "' " +  
                   "WHERE id=" + nID.ToString();

            if (MySQLRunner.ExecuteNonQuery(sSQL, conUpdate) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conUpdate.State == ConnectionState.Open)
            {
                conUpdate.Close();
            }
            return flgReturn;
        }

        private bool Add()
        {
            MySqlConnection conAdd = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "";

            if (MySQLRunner.ExecuteNonQuery(sSQL, conAdd) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conAdd.State == ConnectionState.Open)
            {
                conAdd.Close();
            }

            return flgReturn;
        }

        public bool DeleteObject()
        {
            MySqlConnection conDelete = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "DELETE FROM petitions WHERE petid=" + nID.ToString();

            if (MySQLRunner.ExecuteNonQuery(sSQL, conDelete) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conDelete.State == ConnectionState.Open)
            {
                conDelete.Close();
            }

            return flgReturn;
        }

        private int GetMaxID()
        {
            MySqlConnection conGet = new MySqlConnection(MySQLRunner.ConnectionString);
            MySqlDataReader drGet = null;
            string sSQL = "";
            int nMax = 0;

            sSQL = "SELECT MAX(id) AS ID FROM account";

            if (MySQLRunner.ExecuteQuery(sSQL, conGet, ref drGet) == true)
            {
                drGet.Read();

                if (drGet.HasRows == false)
                {
                    nMax = 0;
                }
                else
                {
                    nMax = drGet.GetInt32(drGet.GetOrdinal("ID"));
                }
            }
            else
            {
                MessageBox.Show("Error connecting to the database.");
            }

            if (conGet.State == ConnectionState.Open)
            {
                if (drGet != null)
                    drGet.Close();

                conGet.Close();
            }

            return nMax;
        }
    }
}
